package com.applet.user.service.impl;

import com.applet.common.constant.Constants;
import com.applet.common.core.page.TableDataInfo;
import com.applet.common.core.redis.RedisCache;
import com.applet.common.utils.DateUtils;
import com.applet.common.utils.SecurityUtils;
import com.applet.common.utils.bean.DataCount;
import com.applet.user.domain.Publish;
import com.applet.user.domain.dto.PublishDto;
import com.applet.user.mapper.PublishMapper;
import com.applet.user.service.IPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 兼职发布Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-16
 */
@Service
public class PublishServiceImpl implements IPublishService {
    @Autowired
    private PublishMapper publishMapper;

    @Autowired
    private RedisCache redisCache;

    private final BlockingQueue<Publish> publishQueue = new LinkedBlockingQueue<>(100);

    /**
     * 查询兼职发布
     *
     * @param id 兼职发布主键
     * @return 兼职发布
     */
    @Override
    public Publish selectPublishById(Long id) {
        return publishMapper.selectPublishById(id);
    }

    @Override
    public List<Publish> selectPublishByUserId() {
        return publishMapper.selectPublishByUserId(SecurityUtils.getUserId());
    }

    @Override
    public List<DataCount> selectPartTimeStatistics() {
        return publishMapper.selectPartTimeStatistics();
    }

    @Override
    public Map<String,List<Object>> selectStatisticsTopFive() {
        List<DataCount> data = publishMapper.selectStatisticsTopFive();
        Map<String,List<Object>> map = new HashMap<>();
        List<String> contentList = new ArrayList<>();
        List<Integer> numberList = new ArrayList<>();
        for (DataCount dataCount : data){
            contentList.add(dataCount.getName());
            numberList.add(dataCount.getValue());
        }
        map.put("contentList", Collections.singletonList(contentList));
        map.put("numberList", Collections.singletonList(numberList));
        return map;
    }

    /**
     * 查询兼职发布列表
     *
     * @param publishDto 兼职发布
     * @return 兼职发布
     */
    @Override
    public List<Publish> selectPublishList(PublishDto publishDto) {
        if (publishDto.getStartTime() != null && publishDto.getEndTime() != null) {
            if (publishDto.getStartTime().equals(publishDto.getEndTime())) {
                //将Data类型转换为String
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String endDate = sdf1.format(publishDto.getEndTime());
                //将String字符串改为当前时间最大值的样式，将改好的String字符串转化为Date类型
                publishDto.setEndTime(DateUtils.parseDate(endDate.substring(0, 10) + " 23:59:59"));
            }
        }
        return publishMapper.selectPublishList(publishDto);
    }

    /**
     * 新增兼职发布
     *
     * @param publish 兼职发布
     * @return 结果
     */
    @Override
    public int insertPublish(Publish publish) {
        publish.setCreateTime(DateUtils.getNowDate());
        //获取用户id
        publish.setPublishUserId(SecurityUtils.getUserId());
        //默认状态为0：正常
        publish.setStatus(Constants.PUBLISH_STATUS_NORMAL);
        if (publish.getDemand().isEmpty()) {//用户没有填写此属性
            publish.setDemand("暂无");
        }
        // 将对象放入队列，应对高并发场景下的优化操作，批量插入
//        try {
//            publishQueue.put(publish);
//        } catch (InterruptedException e) {
//            Thread.currentThread().interrupt();
//        }
//        if (publishQueue.size() >= 100) {
//            List<Publish> list= new ArrayList<>();
//            //从队列中取出数据
//            publishQueue.drainTo(list, publishQueue.size());
//            //进行批量插入
//            return publishMapper.insertBatch(list);
//        }
        return publishMapper.insertPublish(publish);
    }

    /**
     * 修改兼职发布
     *
     * @param publish 兼职发布
     * @return 结果
     */
    @Override
    public int updatePublish(Publish publish) {
        publish.setUpdateTime(DateUtils.getNowDate());
        return publishMapper.updatePublish(publish);
    }

    /**
     * 批量删除兼职发布
     *
     * @param ids 需要删除的兼职发布主键
     * @return 结果
     */
    @Override
    public int deletePublishByIds(Long[] ids) {
        return publishMapper.deletePublishByIds(ids);
    }

    /**
     * 删除兼职发布信息
     *
     * @param id 兼职发布主键
     * @return 结果
     */
    @Override
    public int deletePublishById(Long id) {
        return publishMapper.deletePublishById(id);
    }

    //定时任务，每三秒执行一次批量插入操作
//    @Scheduled(fixedRate = 3000)
//    public void scheduledInsertBatch() {
//        insertBatch();
//    }
}
